<template>
  <table class="table">
    <thead class="table_header_title">
      <tr>
        <th>商品信息</th>
        <th>属性</th>
        <th>单价</th>
        <th>折后单价</th>
        <th>退货数量</th>
        <th>退款小计</th>
        <th>折后退款小计</th>
      </tr>
    </thead>

    <tbody class="table_tbody">
      <tr>
        <td class="noborder" colspan="7"></td>
      </tr>
      <tr>
        <td colspan="7" class="last">
          <div class="info">
            <div class="order">
              <el-checkbox v-if="!route.query.type" v-model="tableData.checked" @change="(val) => onChange(val)"></el-checkbox>
              <span class="order-time">{{ formater.formatDate(tableData.orderInfo.createTime) }}</span>
              <span class="order-code">订单编号：{{ tableData.orderInfo.orderCode }}</span>
            </div>
            <div class="supplier">
              <img src="@/assets/images/dianpuxiao.png" class="store-icon" />
              <span class="supplier-name">供应商：{{ tableData.orderInfo.supplierName }}</span>
            </div>
          </div>
        </td>
      </tr>
      <tr class="goods" v-for="goods in goodsList" :key="goods.orderGoodsId">
        <td>
          <div class="goods-first">
            <el-checkbox v-if="!route.query.type" class="goods-inner-checkbox" v-model="goods.checked" @change="(val) => onRowCheckedChange(val, goods)"></el-checkbox>
            <div class="goods-info">
              <img class="goods-img" v-if="!route.query.type" :src="goods.goodsImg" />
              <span class="goods-name"> {{ goods.goodsName }}</span>
            </div>
          </div>
        </td>
        <td>
          <span>{{ goods.goodsSpec }}</span>
        </td>
        <td>
          <span v-if="!route.query.type">¥{{ goods.price }}</span>
          <span v-else>{{ goods.backGoodsPrice }}</span>
        </td>
        <!-- 折后单价 -->
        <td>
          <span v-if="!route.query.type">¥{{ goods.discountPrice }}</span>
          <span v-else>{{ goods.backDiscountPrice }}</span>
        </td>
        <td class="amount-edit">
          <el-input-number v-if="!route.query.type" size="small" :min="1" :max="goods.maxAmount" v-model="goods.amount" />
          <span v-else>{{ goods.amount }}</span>
        </td>
        <td class="goods-price last">
          <span v-if="!route.query.type">¥{{ (goods.amount * goods.price).toFixed(2) }}</span>
          <span v-else>¥{{ goods.totalPrice }}</span>
        </td>
        <!-- 折后退款小计 -->
        <td class="goods-price last">
          <span v-if="!route.query.type">¥{{ (goods.amount * goods.discountPrice).toFixed(2) }}</span>
          <span v-else>¥{{ goods.totalDiscountPrice }}</span>
        </td>
      </tr>
      <tr>
        <td class="last border" colspan="7">
          <div class="remark">
            <span class="title">退款金额</span>

            <span class="price" v-if="!route.query.type">¥{{ sums }}</span>
            <span class="price" v-else>¥{{ sum1 }}</span>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script setup>
import { reactive, onMounted, ref, toRefs, computed } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import api from '@/api'
import formater from '@/formater'
import Button from '@/components/button/button.vue'

const props = defineProps(['type', 'info', 'list'])
const emit = defineEmits(['change'])

const { info } = toRefs(props)

const route = useRoute()

const tableData = reactive({
  orderInfo: info,
  checkedAll: false,
  checked: false,
  price: ''
})

const goodsList = computed(() => {
  props.list.map((e) => {
    if (!e.maxAmount) {
      e.maxAmount = e.amount
    }
  })
  return props.list
})

const sums = computed(() => {
  let sum = goodsList.value.filter((e) => e.checked === true).map((e) => e.amount * e.discountPrice)
  return (sum.reduce((acc, cur) => cur + acc, 0)).toFixed(2)
})

const sum1 = computed(() => {
  let sum = goodsList.value.map((e) => e.backDiscountPrice * e.amount)
  return (sum.reduce((acc, cur) => cur + acc, 0)).toFixed(2)
})

const onChange = (val) => {
  goodsList.value.forEach((e) => {
    e.checked = val
  })

  emit('change', { checkedGoodsList: goodsList.value, backPrice: sums.value })
}

const onRowCheckedChange = (val) => {
  const checkedGoodsList = goodsList.value.filter((e) => e.checked === true)
  if (checkedGoodsList.length === goodsList.value.length) {
    tableData.checked = true
  } else {
    tableData.checked = false
  }

  emit('change', { checkedGoodsList, backPrice: sums.value })
}
</script>
<style scoped lang="scss">
.table {
  border-collapse: collapse;
  width: calc(100% - 104px);

  th {
    width: 160px;
  }
  .table_header_title {
    height: 42px;
    background-color: var(--color-background);
    color: var(--span-color-text);

    margin-bottom: 20px;
  }

  .table_tbody {
    td.noborder {
      border: 0;
      height: 20px;
    }

    td.noborder:first-child {
      border-left: none;
    }

    tr {
      td {
        line-height: 18px;
        text-align: center;
      }

      td:first-child {
        border-left: 1px solid var(--border-color);
      }

      td.last {
        border-right: 1px solid var(--border-color);
      }

      td.border {
        border-bottom: 1px solid var(--border-color);
        padding: 10px 35px 13px 54px;
        .remark {
          display: flex;
          justify-content: space-between;

          .title {
            font-weight: 500;
            line-height: 16px;
          }

          .price {
            font-weight: 600;
            line-height: 18px;
            color: var(--color-primary);
          }
        }
      }

      td.amount-edit {
        border-left: 1px solid var(--border-color);
        border-right: 1px solid var(--border-color);
        position: relative;
      }

      td.goods-price {
        font-weight: 600;
        color: var(--color-primary);
      }

      .edit-btn {
        position: absolute;
        right: 0;
        top: 0;
        display: none;
        background-color: var(--color-primary);
        color: #fff;
        font-size: 12px;
        border: none;
      }

      td.amount-edit:hover {
        // border: 1px dashed var(--color-primary);

        .edit-btn {
          display: block;
        }
      }
    }

    .info {
      display: flex;
      align-items: center;
      padding: 12px 0 12px 10px;
      height: 42px;
      box-sizing: border-box;
      border-top: 1px solid var(--border-color);
      background-color: #f9f9f9;

      .order,
      .supplier {
        display: flex;
        align-items: center;
        height: 18px;
        line-height: 18px;
      }

      .order {
        .goods-outer-checkbox {
          width: 14px;
          height: 14px;
        }

        .order-time {
          margin: 0 12px;
        }

        .order-code {
          color: var(--span-color-text);
          font-weight: 500;
        }
      }

      .supplier {
        margin-left: 90px;

        .store-icon {
          width: 20px;
          height: 20px;
          margin: 0 7px 0 11px;
        }
      }
    }

    .goods {
      td {
        border-bottom: 1px solid var(--border-color);
        padding: 20px 0 20px 10px;

        .goods-first {
          display: flex;
          align-items: center;

          .goods-info {
            display: flex;
            align-items: center;

            .goods-img {
              display: inline-block;
              width: 72px;
              height: 72px;
              margin-right: 12px;
              margin-left: 30px;
            }

            .goods-name {
              width: 180px;
              line-height: 18px;
            }
          }
        }
      }
    }
  }
}

.el-input-number :deep(.el-input--small) {
  width: 124px;
  height: 32px;
}

.el-input-number--small {
  width: 124px;
  height: 32px;

  :deep(.el-input-number__decrease),
  :deep(.el-input-number__increase) {
    width: 30px;
    height: 30px;
    box-sizing: border-box;
  }
}
</style>
